from xml.dom import minidom
from PhotoUtils import *
from QA import *

def process_qa(pdom):
  qsdom = pdom.getElementsByTagName('question')
  questions = []
  for qdom in qsdom:
    question = Question(qdom.attributes['text'].value)
    asdom = qdom.getElementsByTagName('answer')
    for adom in asdom:
      answer = Answer(adom.attributes['text'].value, (adom.attributes['result'].value == "correct"))
      question.addAnswer(answer)
    questions.append(question)
  return questions


def process_wall(wdom, parent):
  cols = int(wdom.attributes["columns"].value)
  rows = int(wdom.attributes["rows"].value)
  x = int(wdom.attributes["x"].value)
  y = int(wdom.attributes["y"].value)
  rot = int(wdom.attributes["rotation"].value)
  pw = PhotoWall(cols, rows)

  psdom = wdom.getElementsByTagName("photo")
  for pdom in psdom:
    print ".",
    pnp = pw.addPhotoPath(pdom.attributes['path'].value)
    questions = process_qa(pdom)
    pnp.setPythonTag('questions', questions)

  pwnp = pw.getNodePath()
  pwnp.setPos(x,y,0)
  pwnp.setH(rot)
  pwnp.reparentTo(parent)


def process_walls(doc, parent):
  wsdom = doc.getElementsByTagName("wall")
  print "loading pictures"
  for wdom in wsdom:
    process_wall(wdom, parent)


def parse_config(parent, config_file="config/pr3da.conf"):
  doc = minidom.parse(config_file)
  process_walls(doc, parent)

  print ""
  print ""